package com.zgl.modules.card.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zgl.modules.card.entity.CardEntity;
import com.zgl.modules.card.mapper.CardMapper;
import com.zgl.modules.card.service.CardService;
import com.zgl.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {
    @Resource
    CardMapper cardMapper;

    public R savaOne(CardEntity cardEntity) {
        if (1 == cardMapper.insert(cardEntity)) {
            return R.success();
        } else {
            return R.error("操作失败");
        }
    }

    public R removeOne(Integer id) {
        if (1 == cardMapper.deleteById(id)) {
            return R.success();
        } else {
            return R.error("操作失败");
        }
    }

    public R modifyOne(CardEntity cardEntity) {
        if (cardMapper.updateById(cardEntity) == 1) {
            return R.success();
        } else {
            return R.error();
        }

    }

    public R getOne(Integer id) {
        CardEntity CardEntity = cardMapper.selectById(id);
        return R.success(CardEntity);
    }

    @Override
    public R getCardByUserId(String id) {
        QueryWrapper<CardEntity> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("userid",id).eq("used",1);
        List<CardEntity> cardEntityList=cardMapper.selectList(queryWrapper);
        return R.success(cardEntityList);
    }
    @Override
    public R getCardByUserNumber(String number) {
        QueryWrapper<CardEntity> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("card_number",number);
        CardEntity cardEntity=cardMapper.selectOne(queryWrapper);
        return R.success(cardEntity);
    }
}
